package com.ruoyi.miniapp.service.impl;

import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.miniapp.domain.MiniActivity;
import com.ruoyi.miniapp.domain.MiniActivitySignup;
import com.ruoyi.miniapp.domain.MiniChildInfo;
import com.ruoyi.miniapp.domain.MiniGroupInfo;
import com.ruoyi.miniapp.domain.vo.ActivitySignupVO;
import com.ruoyi.miniapp.mapper.MiniActivityMapper;
import com.ruoyi.miniapp.mapper.MiniActivitySignupMapper;
import com.ruoyi.miniapp.mapper.MiniChildInfoMapper;
import com.ruoyi.miniapp.mapper.MiniGroupInfoMapper;
import com.ruoyi.miniapp.service.IMiniActivityService;
import com.ruoyi.miniapp.service.IMiniActivitySignupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 * 活动报名关联Service业务层处理
 * 
 * @author duantao
 * @date 2025-09-19
 */
@Service
public class MiniActivitySignupServiceImpl implements IMiniActivitySignupService
{

    @Autowired
    private IMiniActivityService miniActivityService;
    @Autowired
    private MiniActivityMapper miniActivityMapper;
    @Autowired
    private MiniActivitySignupMapper miniActivitySignupMapper;
    @Autowired
    private MiniChildInfoMapper miniChildInfoMapper;
    @Autowired
    private MiniGroupInfoMapper miniGroupInfoMapper;

    /**
     * 查询活动报名关联
     * 
     * @param id 活动报名关联主键
     * @return 活动报名关联
     */
    @Override
    public MiniActivitySignup selectMiniActivitySignupById(Long id)
    {
        return miniActivitySignupMapper.selectMiniActivitySignupById(id);
    }

    /**
     * 查询活动报名关联列表
     * 
     * @param miniActivitySignup 活动报名关联
     * @return 活动报名关联
     */
    @Override
    public List<MiniActivitySignup> selectMiniActivitySignupList(MiniActivitySignup miniActivitySignup)
    {
        return miniActivitySignupMapper.selectMiniActivitySignupList(miniActivitySignup);
    }



    /**
     *  检查报名人数是否超出活动报名人数限制
     * @param signupVO
     * @return true-已超出限制，false-还有名额
     *
     */
    private boolean checkLimit(ActivitySignupVO signupVO){
        MiniActivity miniActivity = miniActivityMapper.selectMiniActivityByActivityId(signupVO.getActivityId());
        Integer limit = miniActivity.getActivityLimit();
        // 为空代表不限制人数
        if (ObjectUtils.isEmpty(limit)){
            return false;
        }

        // 获取剩余名额
        int leavings = miniActivityService.getActivityLeavings(signupVO.getActivityId());

        // 剩余名额+本次报名人数不能超过limit
        if ( leavings > 0){
            int thisTime = 0;
            if("0".equals(signupVO.getSignUpType())){
                thisTime = signupVO.getChildren().size();
            }else {
                thisTime = signupVO.getGroup().getGroupPersonNum().intValue();
            }

            int i = leavings - thisTime;
            // 1.剩余名额-本次报名人数 > 0 false  仍有名额
            // 2.剩余名额-本次报名人数 = 0 false  刚好足额
            // 3.剩余名额-本次报名人数 < 0 true   名额不足
            return i < 0;
        }else {
           return true;
        }
    }
    /**
     * 新增活动报名关联
     *
     * @param signupVO 活动报名VO
     * @return 结果
     */
    @Override
    public MiniActivitySignup insertMiniActivitySignup(ActivitySignupVO signupVO)
    {
        // 判断报名人数是否达到上限
        boolean checkLimit = checkLimit(signupVO);
        if (checkLimit){
            throw new RuntimeException("超出活动报名人数限制");
        }


        MiniActivitySignup miniActivitySignup = new MiniActivitySignup();

        //判断报名类型-个人报名/团队报名
        String signUpType = signupVO.getSignUpType();
        if ("0".equals(signUpType)){
            //个人报名
            // 记录孩子信息
            List<MiniChildInfo> children = signupVO.getChildren();
            for (MiniChildInfo child : children) {
                miniActivitySignup = new MiniActivitySignup();
                miniActivitySignup.setActivityId(signupVO.getActivityId());
                miniActivitySignup.setSignUpType(signupVO.getSignUpType());
                miniActivitySignup.setCreateTime(DateUtils.getNowDate());

                miniChildInfoMapper.insertMiniChildInfo(child);
                Long childId = child.getChildId();
                miniActivitySignup.setChildrenId(childId);

                miniActivitySignupMapper.insertMiniActivitySignup(miniActivitySignup);
            }

        }else if ("1".equals(signUpType)){
            miniActivitySignup = new MiniActivitySignup();
            miniActivitySignup.setActivityId(signupVO.getActivityId());
            miniActivitySignup.setSignUpType(signupVO.getSignUpType());
            miniActivitySignup.setCreateTime(DateUtils.getNowDate());
            //团队报名
            MiniGroupInfo group = signupVO.getGroup();
            miniGroupInfoMapper.insertMiniGroupInfo(group);
            miniActivitySignup.setGroupId(group.getGroupId());
            miniActivitySignup.setGroupPersonNum(signupVO.getGroup().getGroupPersonNum());
            miniActivitySignupMapper.insertMiniActivitySignup(miniActivitySignup);
        }

        return miniActivitySignup;
    }

    /**
     * 修改活动报名关联
     * 
     * @param miniActivitySignup 活动报名关联
     * @return 结果
     */
    @Override
    public int updateMiniActivitySignup(MiniActivitySignup miniActivitySignup)
    {
        miniActivitySignup.setUpdateTime(DateUtils.getNowDate());
        return miniActivitySignupMapper.updateMiniActivitySignup(miniActivitySignup);
    }

    /**
     * 批量删除活动报名关联
     * 
     * @param ids 需要删除的活动报名关联主键
     * @return 结果
     */
    @Override
    public int deleteMiniActivitySignupByIds(Long[] ids)
    {
        return miniActivitySignupMapper.deleteMiniActivitySignupByIds(ids);
    }

    /**
     * 删除活动报名关联信息
     * 
     * @param id 活动报名关联主键
     * @return 结果
     */
    @Override
    public int deleteMiniActivitySignupById(Long id)
    {
        return miniActivitySignupMapper.deleteMiniActivitySignupById(id);
    }
}
